﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using Cqrs.Infrastructure;
using Cqrs.Infrastructure.Database;
using EventStore;
using Meditime.Domain;
using Meditime.UI;
using domain = Meditime.Domain;
using readmodel = Meditime.UI;

namespace Meditime.UI
{
    public partial class AddEditMember : Form
    {

        private Guid _id;
        private int _version;
        private Action _execCommand;

        public AddEditMember()
        {
            InitializeComponent();
        }

        public Action EditMember(Guid staffId)
        {
            btnSave.Enabled = false;
            btnCancelClose.Text = "Close";
            this.Width = 581;

            using (ReadModelEntities rm = new ReadModelEntities())
            {
                var member = (from sm in rm.StaffMembers
                              where sm.Id == staffId
                              select sm).FirstOrDefault();
                txtName.Text = member.Name;
                dtDOB.Value = member.DateOfBirth;
                _id = staffId;
                _version = member.Version;

                llbUpdateName.Enabled = true;
                llbUpdateDOB.Enabled = true;
                llblMarkAsInactive.Enabled = true;

                if (member.Status == domain.StaffMember.States.Created)
                {
                    llbMakePermanent.Enabled = true;
                    llbMakeContract.Enabled = true;
                }
                if (member.Status == domain.StaffMember.States.Contracted)
                    llbMakePermanent.Enabled = true;
                else if (member.Status == domain.StaffMember.States.FullTime)
                    llbMakeContract.Enabled = true;
            }
            this.ShowDialog();
            return _execCommand;
        }

        public Action CreateStaffMember()
        {
            grpActions.Enabled = false;
            this.Width = 350;
            this.ShowDialog();
            return _execCommand;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                CreateStaffMember createStaffMember = new CreateStaffMember(Guid.NewGuid(), txtName.Text, dtDOB.Value, 0);
                _execCommand = () => StaffMemberCommands.Instance().Submit(createStaffMember);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void llbMakePermanent_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                MakeFulltime makeFullTime = new MakeFulltime(_id, _version);
                _execCommand = () => StaffMemberCommands.Instance().Submit(makeFullTime);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void llbMakeContract_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                MakeContract makeContract = new MakeContract(_id, _version);
                _execCommand = () => StaffMemberCommands.Instance().Submit(makeContract);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancelClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch { }
        }

        private void llblMarkAsInactive_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                MarkAsInactive makeInactive = new MarkAsInactive(_id, _version);
                _execCommand = () => StaffMemberCommands.Instance().Submit(makeInactive);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void llbUpdateName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                UpdateName updateName = new UpdateName(_id, txtName.Text, _version);
                _execCommand = () => StaffMemberCommands.Instance().Submit(updateName);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void llbUpdateDOB_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                UpdateDateOfBirth updateDOB = new UpdateDateOfBirth(_id, dtDOB.Value, _version);
                _execCommand = () => StaffMemberCommands.Instance().Submit(updateDOB);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
